# -*- encoding: utf-8 -*-
"""
@Project : AIGateWay 
@FileName: async_handler
@Time    : 2020/11/26 14:59
@Author  : linych
@Desc    :
"""

from Client.asyn_request import AsynClient
from Core import SYS_CODE
from Core import conf
from util.check_resp_util import RespException,asyn_check_resp
import json
import ailog



class FaceDeletTask():

    host = conf.get('face_ability_url', 'host_face')
    port = conf.get('face_ability_url', 'port_face')

    url_delete_index = 'http://{}:{}{}'.format(host, port, conf.get('face_ability_url','url_delete_index_base'))
    url_delete_face = 'http://{}:{}{}'.format(host, port, conf.get('face_ability_url','url_delete_face_base'))

    asynClient = AsynClient()


    @staticmethod
    def __build_resp(seqid,code,message,flag):

        resp_dict = {

            'seqid': seqid,
            'code': code,
            'message': message,
            'flag': flag,
        }

        return resp_dict


    @classmethod
    async def __del_index(cls, deviceId):

        data = {
            'group_id':deviceId
        }

        header = {'Content-type': 'application/json'}

        res_device = await cls.asynClient.post(url=cls.url_delete_index,body=json.dumps(data),header=header)

        return res_device

    @classmethod
    @asyn_check_resp(success_code=SYS_CODE.STATUS.ABILITY_SUCCESS,code_name='code',msg_name='message')
    async def __del_face(cls, deviceId, visitorId):

        data= {
            'group_id':deviceId,
            'ext_id':visitorId

        }

        header = {'Content-type': 'application/json'}

        res_visitor = await  cls.asynClient.post(url=cls.url_delete_face, body=json.dumps(data),header=header)

        res_visitor = json.loads(res_visitor)

        return res_visitor

    @classmethod
    async def run(cls, param):

        seqid = param['seqid']

        try:
            deviceId = param['deviceId']

            visitorId = None if 'visitorId' not in param else param['visitorId']

        except Exception as e:

            print('缺少参数：{}'.format(e))

            resp_dict = cls.__build_resp(seqid=seqid,
                                         code=SYS_CODE.RESPCODE.KEYERROR,
                                         message=SYS_CODE.RESPCODE.get_msg(SYS_CODE.RESPCODE.KEYERROR),
                                         flag=SYS_CODE.RESPCODE.FLAG_FAIL)
            return resp_dict

        print(deviceId,visitorId)
        try:

            if visitorId is not None:

                res = await cls.__del_face(deviceId=deviceId,visitorId=visitorId)
            else:
                res = await cls.__del_index(deviceId=deviceId)



            resp_dict = cls.__build_resp(seqid=seqid,
                                         code=SYS_CODE.RESPCODE.SUCCESS,
                                         message=SYS_CODE.RESPCODE.get_msg(SYS_CODE.RESPCODE.SUCCESS),
                                         flag=SYS_CODE.RESPCODE.FLAG_SUCCESS)
            return resp_dict

        except RespException as er:
            ailog.error('resp error : {}'.format(er))

            resp_dict = cls.__build_resp(seqid=seqid,
                                         code=er.code,
                                         message=er.msg,
                                         flag=SYS_CODE.RESPCODE.FLAG_FAIL)

            return resp_dict


        except Exception as e:
            ailog.error('error : {}'.format(e))

            resp_dict = cls.__build_resp(seqid=seqid,
                                         code=SYS_CODE.RESPCODE.FAIL,
                                         message=SYS_CODE.RESPCODE.get_msg(SYS_CODE.RESPCODE.FAIL),
                                         flag=SYS_CODE.RESPCODE.FLAG_FAIL)

            return resp_dict



